const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    /**范围显示的框选节点 */
    range = new cc.Node();

    /**第一个手指点击位置 */
    firstTouch: cc.Vec2 = null;
    /**第二个手指点击位置 */
    secondTouch: cc.Vec2 = null;

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
    }

    touchStart(event: cc.Event.EventTouch) {
        console.error(event.getID(), event.getLocation());
        if (event.getID() == 0) {
            this.firstTouch = event.getLocation();
        }
        if (event.getID() == 1) {
            this.secondTouch = event.getLocation();
            this.rangeChange();
        }
    }

    touchMove(event: cc.Event.EventTouch) {
        console.error(event.getID(), event.getLocation());
        if (event.getID() == 0) {
            this.firstTouch = event.getLocation();
        }
        if (event.getID() == 1) {
            this.secondTouch = event.getLocation();
        }
        this.rangeChange();
    }

    touchEnd(event: cc.Event.EventTouch) {
        console.error(event.getTouches(), event.getID());
        this.range.active = false;
        if (event.getID() == 0) {
            this.firstTouch = null;
        }
        if (event.getID() == 1) {
            this.secondTouch = null;
        }
    }

    /**框选范围变化 */
    rangeChange() {
        if (this.firstTouch && this.secondTouch) {
            //开始显示框选节点
            this.range.width = Math.abs(this.firstTouch.x - this.secondTouch.x);
            this.range.height = Math.abs(this.firstTouch.y - this.secondTouch.y);
            let pos = cc.v2((this.firstTouch.x + this.secondTouch.x) / 2, (this.firstTouch.y + this.secondTouch.y) / 2);
            this.range.position = this.node.convertToNodeSpaceAR(pos);
            this.range.active = true;
        }
    }

}